package com.atwisdom.star.service.business;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.util.SpringContextUtil;
import com.atwisdom.star.core.dao.pojo.modelClass.hier.HierInstance;
import com.atwisdom.star.core.dao.pojo.modelClass.twins.TwinsInstance;
import com.atwisdom.star.core.dsl.modelClass.consume.Impl.ConsumeInstanceDslServiceImpl;
import com.atwisdom.star.core.dsl.modelClass.twins.Impl.TwinsInstanceDslServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Panhao
 * @ClassName TwinsConsume
 * @description TODO
 * @date 2024/5/12
 * lastUpdate Panhao
 */
@Service
public class TwinsConsume extends Business{
    private final TwinsInstanceDslServiceImpl containerInstanceService  = SpringContextUtil.getBean(TwinsInstanceDslServiceImpl.class);
    private final ConsumeInstanceDslServiceImpl childInstanceService  = SpringContextUtil.getBean(ConsumeInstanceDslServiceImpl.class);
    private TwinsInstance containerInstance;
    private List<HierInstance> containerHierInstances;
    private List<JSONObject> childInstances;

    private TwinsInstance template;
    private List<HierInstance> templateHierInstances;

    private final String containerModel = "twins";
    private final String childModel = "consume";

    private JSONObject getTemplateStatus(){
        return JsonUtil.buildJson(new Object[] {"if_template",template.getId(),"bf_template",0,"sf_status","创建"});
    }

    private JSONObject getHierTemplateStatus(HierInstance hierItem){
        return JsonUtil.buildJson(new Object[] {"if_template",hierItem.getLinkId(),"bf_template",0});
    }

    /**
     *根据id初始化数据
     * @param id 容器id
     */
    public void setContainer(Long id){
        setContainer(containerInstanceService.queryInsWithHierById(id));
    }

    /**
     *根据id初始化数据
     * @param container 容器实例
     */
    public void setContainer(TwinsInstance container){
        this.containerInstance = container;
        containerHierInstances = containerInstance.getHierInstances().stream().filter(hierInstance -> childModel.equals(hierInstance.getLinkModel())).collect(Collectors.toList());
        List<Long> ids = containerHierInstances.stream().map(HierInstance::getLinkId).collect(Collectors.toList());
        setChild(ids);
    }

    /**
     * 根据id初始化模板数据
     * @param id 模板容器id
     */
    public void setTemplate(Long id){
        setTemplate(containerInstanceService.queryInsWithHierById(id));
    }

    /**
     * 根据id初始化模板数据
     * @param template 模板数据
     */
    public void setTemplate(TwinsInstance template){
        this.template = template;
        templateHierInstances = template.getHierInstances().stream().filter(hierInstance -> childModel.equals(hierInstance.getLinkModel())).collect(Collectors.toList());
    }


    /**
     * 初始化子节点实例数据
     * @param ids 子节点id集合
     */
    public void setChild(List<Long> ids){
        if(CollectionUtil.isEmpty(ids)){
            return;
        }
        childInstances = childInstanceService.queryByIds(ids);
    }
}
